﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using ZoneLineNew.Models.Dto;
using ZoneLineNew.Models.Entity.User;
using ZoneLineNew.Views;

namespace ZoneLineNew.Common {
    /// <summary>
    /// 全局应用信息
    /// </summary>
    public class AppSession {

        /// <summary>
        /// 应用名称
        /// </summary>
        public static string AppactionName => ConfigurationManager.AppSettings["AppactionName"]?.ToString();

        /// <summary>
        /// 应用版本
        /// </summary>
        public static string Version => ConfigurationManager.AppSettings["Version"]?.ToString();

        /// <summary>
        /// 菜单列表
        /// </summary>
        //public static ObservableCollection<MenuItemDto> MenuList => new ObservableCollection<MenuItemDto>()
        //{
        //    new MenuItemDto{ Name = "生产车间", Code = nameof(MainView), Icon ="", 
        //        Children = new ObservableCollection<MenuItemDto>()
        //        {
        //             new MenuItemDto{ Name = "生产车间", Code = nameof(MainView), Icon ="" },
        //             new MenuItemDto{ Name = "数据查询", Code = nameof(DataHistoryView), Icon ="" }
        //        }
        //    },
        //    new MenuItemDto{ Name = "数据查询", Code = nameof(DataHistoryView),Icon = "", Children = new ObservableCollection<MenuItemDto>()},
        //    //new MenuItemDto{ Name = "标签打印", Code = nameof(LabelView),Icon = "", },
        //    //new MenuItemDto{ Name = "系统设置", Code = nameof(SettingView),Icon = "", },
        //};

        /// <summary>
        /// 当前用户
        /// </summary>
        public static UserEntity CurrentUser { get; set; }

        /// <summary>
        /// AppConfig配置
        /// </summary>
        public static AppConfig Config { get; set; }

        /// <summary>
        /// GrowToken
        /// </summary>
        public static string GrowToken => "MainViewGrowToken";


        /// <summary>
        /// 初始化AppConfig
        /// </summary>
        public static void InitAppConfig() {
            if (File.Exists(AppConfig.SavePath)) {
                try {
                    var json = File.ReadAllText(AppConfig.SavePath);
                    Config = (string.IsNullOrEmpty(json) ? new AppConfig() : JsonConvert.DeserializeObject<AppConfig>(json)) ?? new AppConfig();
                } catch {
                    Config = new AppConfig();
                }
            } else {
                Config = new AppConfig();
            }
        }

        /// <summary>
        /// 保存AppConfig，自动重新初始化
        /// </summary>
        public static void SaveAppConfig() {
            var json = JsonConvert.SerializeObject(Config, Newtonsoft.Json.Formatting.Indented);
            File.WriteAllText(AppConfig.SavePath, json);
            InitAppConfig();
        }

    }
}
